package cn.xgxd.service.Impl;
import cn.xgxd.mapper.GoodsMapper;
import cn.xgxd.pojo.Goods;
import cn.xgxd.service.GoodsService;
import cn.xgxd.utils.UserContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    private UserContext userContext;

    @Override
    public Map showGoods(Integer page) { //授权的商品
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        wrapper.eq("allow",1);
        Page<Goods> pages = new Page<>(page, 16);
        goodsMapper.selectPage(pages, wrapper);
        map.put("data",pages.getRecords());
        map.put("count",pages.getTotal());
        return map;
    }

    @Override
    public Map allowGoods(Integer page) { //未授权的商品
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        wrapper.eq("allow",0);
        Page<Goods> pages = new Page<>(page, 16);
        goodsMapper.selectPage(pages, wrapper);
        map.put("data",pages.getRecords());
        map.put("count",pages.getTotal());
        return map;
    }

    @Override
    public Goods selectOneGoods(String id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        wrapper.eq("id",id);
        return goodsMapper.selectOne(wrapper);
    }

    @Override
    public int selectGoodsCount() {
        return  goodsMapper.selectCount(null);
    }

    @Override
    public void swAllow(Goods goods) {
         goodsMapper.updateById(goods);
    }

    @Override
    public void addGoods(Goods goods) {
        goods.setImg("https://images.cardhobby.com/4740190e24694c66b3c9d117d41ab8f8.png?imageView2/2/w/700");
        goods.setTime(new Date());
        goods.setUserId(userContext.getUserId());
        if (goods.getType().isEmpty()){
            goods.setType("other");
        }
        goodsMapper.insert(goods);
    }

    @Override
    public List basketball() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        wrapper.eq("type","basketball");
        return  goodsMapper.selectList(wrapper);
    }

    @Override
    public List football() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        wrapper.eq("type","football");
        return  goodsMapper.selectList(wrapper);
    }

    @Override
    public List my() {
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        wrapper.eq("user_id",userContext.getUserId());
        return  goodsMapper.selectList(wrapper);
    }

    @Override
    public void delGoods(String id) {
      goodsMapper.deleteById(id);

    }
}
